import add from './medal-add.js';
import del from './medal-del.js';
import update from './medal-update.js';

let datagrid = document.querySelector("._datagrid");
let searchForm=document.forms.searchForm;
let seId = searchForm.seId;

fetch("SportingEvents-list.let").then(r=>r.json()).then(data=>{
    data.forEach(item=>{
        seId.add(new Option(item.seName,item.seId));
    });
});

let ms = ["","金牌","银牌","铜牌"]
/**
 * 加载数据
 */
let loadData = function () {
    fetch("medal-list.let").then((r) => r.json()).then((data) => {
        console.debug(data);
        datagrid.innerHTML = "";
        data.forEach((item, index) => {
            let tr = document.createElement("tr");
            tr.innerHTML = `
            <td>${item.seName}</td>
            <td>${item.countryName}</td>
            <td>${item.sportsmanName}</td>
            <td>${ms[item.medalNum]}</td>
            <td>${item.medalResult}</td>
            <td>${item.medalDate}</td>
            <td class="p-2"><div class="btn-group btn-group-sm">
                <button type="button" class="btn btn-primary _update" title="修改"><i class="fa fa-edit"></i></button>
                <button type="button" class="btn btn-danger _delete" title="删除"><i class="fa fa-remove"></i></button>
            </div></td>`;
            datagrid.appendChild(tr);
            tr.querySelector("._update").onclick=function(){
                update(item);
            }
            tr.querySelector("._delete").onclick=function(){
                del(item);
            }
        });
    }).catch((e) => {
        console.error(e);
    });
}
loadData();
window.addEventListener("message",function (e) {
    console.debug(e);
    if(e.data=="loadData"){
        loadData();
    }
});
add();